using System;
using System.Data;
using System.Data.OleDb;
using PCSComUtils.Common;
using PCSComUtils.DataAccess;

using PCSComUtils.PCSExc;

namespace PCSComPayroll.DS
{
	public class PAY_PaymentRequestDetailDS
	{
		public PAY_PaymentRequestDetailDS()
		{
		}

		private const string THIS = "PCSComPayroll.DS.DS.PAY_PaymentRequestDetailDS";
		public void Add(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";

			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				PAY_PaymentRequestDetailVO objObject = (PAY_PaymentRequestDetailVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);

				strSql = "INSERT INTO PAY_PaymentRequestDetail("
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD + ","
					+ PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTDATE_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD + ")"
					+ "VALUES(?,?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD].Value = objObject.PaymentRequestMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD].Value = objObject.PrdocutionLineID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.REQUESTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.REQUESTDATE_FLD].Value = objObject.RequestDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD].Value = objObject.RequestAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD].Value = objObject.ApprovalAmount;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			string strSql = String.Empty;
			strSql = "DELETE " + PAY_PaymentRequestDetailTable.TABLE_NAME + " WHERE  " + "PaymentRequestDetailID" + "=" + pintID.ToString();
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
				ocmdPCS = null;

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}


			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void DeleteByMaster(int pintID)
		{
			const string METHOD_NAME = THIS + ".DeleteByMaster()";
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = "DELETE " + PAY_PaymentRequestDetailTable.TABLE_NAME
					+ " WHERE  " + PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD + "=" + pintID.ToString();
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
				ocmdPCS = null;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}


			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public object GetObjectVO(int pintID)
		{
			const string METHOD_NAME = THIS + ".GetObjectVO()";
			DataSet dstPCS = new DataSet();

			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;
				strSql = "SELECT "
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD + ","
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD + ","
					+ PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTDATE_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD
					+ " FROM " + PAY_PaymentRequestDetailTable.TABLE_NAME
					+ " WHERE " + PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD + "=" + pintID;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				odrPCS = ocmdPCS.ExecuteReader();

				PAY_PaymentRequestDetailVO objObject = new PAY_PaymentRequestDetailVO();

				while (odrPCS.Read())
				{
					objObject.PaymentRequestDetailID = int.Parse(odrPCS[PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD].ToString());
					objObject.PaymentRequestMasterID = int.Parse(odrPCS[PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD].ToString());
					objObject.PrdocutionLineID = int.Parse(odrPCS[PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD].ToString());
					objObject.RequestDate = DateTime.Parse(odrPCS[PAY_PaymentRequestDetailTable.REQUESTDATE_FLD].ToString());
					objObject.RequestAmount = Decimal.Parse(odrPCS[PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD].ToString());
					objObject.ApprovalAmount = Decimal.Parse(odrPCS[PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD].ToString());

				}
				return objObject;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Update(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".Update()";

			PAY_PaymentRequestDetailVO objObject = (PAY_PaymentRequestDetailVO) pobjObjecVO;


			//prepare value for parameters
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				strSql = "UPDATE PAY_PaymentRequestDetail SET "
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD + "=   ?" + ","
					+ PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD + "=   ?" + ","
					+ PAY_PaymentRequestDetailTable.REQUESTDATE_FLD + "=   ?" + ","
					+ PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD + "=   ?" + ","
					+ PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD + "=  ?"
					+ " WHERE " + PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD + "= ?";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD].Value = objObject.PaymentRequestMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD].Value = objObject.PrdocutionLineID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.REQUESTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.REQUESTDATE_FLD].Value = objObject.RequestDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD].Value = objObject.RequestAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD].Value = objObject.ApprovalAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD].Value = objObject.PaymentRequestDetailID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}

		public DataSet List()
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();


			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;

				strSql = "SELECT "
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD + ","
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD + ","
					+ PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTDATE_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD
					+ " FROM " + PAY_PaymentRequestDetailTable.TABLE_NAME;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS, PAY_PaymentRequestDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}


		}

		public DataSet List(int pintMasterID)
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();


			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = "SELECT PaymentRequestDetailID, PaymentRequestMasterID, D.ProductionLineID, L.Code,"
					+ " RequestDate, RequestAmount, ApprovalAmount, UserID"
					+ " FROM PAY_PaymentRequestDetail D JOIN PRO_ProductionLine L ON D.ProductionLineID = L.ProductionLineID"
					+ " WHERE D.PaymentRequestMasterID = " + pintMasterID;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS, PAY_PaymentRequestDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}


		}

		public void UpdateDataSet(DataSet pData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS = null;
			OleDbCommandBuilder odcbPCS;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();

			try
			{
				strSql = "SELECT "
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTDETAILID_FLD + ","
					+ PAY_PaymentRequestDetailTable.PAYMENTREQUESTMASTERID_FLD + ","
					+ PAY_PaymentRequestDetailTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTDATE_FLD + ","
					+ PAY_PaymentRequestDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_PaymentRequestDetailTable.USERID_FLD + ","
					+ PAY_PaymentRequestDetailTable.APPROVALAMOUNT_FLD
					+ " FROM " + PAY_PaymentRequestDetailTable.TABLE_NAME;


				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				pData.EnforceConstraints = false;
				odadPCS.Update(pData, PAY_PaymentRequestDetailTable.TABLE_NAME);

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}

		public DataTable ListAllRequest(int pintSaleOrderDetailID, int pintProductionLineID)
		{
			const string METHOD_NAME = THIS + ".ListAllRequest()";
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = "SELECT SUM(RequestAmount) RequestAmount, RequestType"
					+ " FROM PAY_PaymentRequestMaster M JOIN PAY_PaymentRequestDetail D"
					+ " ON M.PaymentRequestMasterID = D.PaymentRequestMasterID"
					+ " WHERE M.ProductionLineID = " + pintProductionLineID
					+ " AND M.SaleOrderDetailID = " + pintSaleOrderDetailID
					+ " GROUP BY RequestType";
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				DataTable dtbData = new DataTable(PAY_PaymentRequestDetailTable.TABLE_NAME);

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dtbData);

				return dtbData;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}
	}
}